<template>
    <div>
        <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm"
            status-icon>
            <el-form-item label="商品名称" prop="name">
                <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="商品副标题" prop="name">
                <el-input v-model="formData.subName" />
            </el-form-item>
            <el-form-item label="商品图片" prop="img">
                <el-input v-model="formData.img" />
                <Tuku v-model="formData.img"></Tuku>
            </el-form-item>
            <el-form-item label="商品价格" prop="price">
                <!-- <el-input-number v-model="formData.price" :min="1" :max="10" /> -->
                <el-input-number v-model="formData.price" :precision="2" :step="0.1" />
            </el-form-item>
            <el-form-item label="商品排序" prop="seq">
                <el-input-number v-model="formData.seq" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="父目录ID" prop="categoryId">

                <el-tree-select v-model="formData.categoryId" :data="categorytreedata" check-strictly
                    :render-after-expand="false" />
            </el-form-item>
            <el-form-item label="添加人" prop="lastUpdateBy">
                <el-input v-model="formData.lastUpdateBy" />
            </el-form-item>
            <el-form-item label="商品介绍" prop="brief">
                <el-input v-model="formData.brief" />
            </el-form-item <el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    提交
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import Tuku from '../../components/TuKu.vue'
import { CategoryApi,productApi} from '@/api'
const ruleFormRef = ref()
import { ElMessage } from 'element-plus'
import {useRouter} from 'vue-router'
const router= useRouter()

//添加表单的属性定义
const formData= reactive({
    name: '',
    subName: '',
    img: '',
    seq: '',
    categoryId: 0,
    price: 0,
    brief:'',
    lastUpdateBy:''
})

//添加表单的校验规则定义

const rules = reactive({
    name: [
        { required: true, message: '请输入2to10个字符', trigger: 'blur' },
        { min: 2, max: 10, message: '字符输入规则是 2 to 10 个', trigger: 'blur' },
    ],
})
//树形选择器
const categorytreedata = ref([
  {
    value: 0,
    label: '一级目录',
  },
])
onMounted(() => {
    testhezi()
    callCategoryApi()
})
const testhezi = () => { 
    let hzh = ['口红', '首饰', '晒红', '粉扑', '粉底液']
    const cp=hzh.reduce((hezi: any, currentvalue: any) => {
        if(currentvalue === '口红'){hezi.push(currentvalue)}
        if(currentvalue === '粉底液'){hezi.push(currentvalue)}
        return hezi
    }, [])
    console.log(cp);
    
}
//api调用 调用分类数据
const callCategoryApi = () => {
    CategoryApi.select.cell().then((res: any) => {
        res.forEach((item:any) => {
            item.value = item.id
            item.label = item.name
        });
   const categorydata = res.reduce((hezi:any,currentvalue:any)=>{
       currentvalue.children = res.filter((item: any) => item.parentId === currentvalue.id)
       if (currentvalue.parentId === 0) {
        hezi.push(currentvalue)
      }
      return hezi
   }, [])
    console.log(categorydata);
   categorytreedata.value =categorydata
    })
}

const formsubmit = () => { 
    productApi.insert.cell({
        name: formData.name,
        subName: formData.subName,
        img: formData.img,
        seq: formData.seq,
        price: formData.price,
        brief: formData.brief,
        categoryId: formData.categoryId,
        lastUpdateBy: formData.lastUpdateBy
    }).then((res: any) => {
        ElMessage.success('添加成功!',res.msg)
        //跳转到列表页面
        router.push({ name: 'product-list' })
        //更新列表
        callCategoryApi()
    })
}


//表单提交
const submitForm = async (formEl:any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields:any) => {
        if (valid) {
            console.log('submit!')
            formsubmit()
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl:any) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>

<style scoped>
.el-select{
    width:160px;
}
</style>